package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ma.domain.*;
import com.ruoyi.ma.domain.bo.MaMaterialManifestBo;
import com.ruoyi.ma.domain.bo.MaMaterialSupplierBo;
import com.ruoyi.ma.domain.vo.*;
import com.ruoyi.ma.mapper.MaMaterialManifestMapper;
import com.ruoyi.ma.mapper.MaMaterialSupplierMapper;
import com.ruoyi.ma.service.IMaMaterialManifestService;
import com.ruoyi.ma.service.IMaMaterialSupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaMaterialProgressDetailsBo;
import com.ruoyi.ma.mapper.MaMaterialProgressDetailsMapper;
import com.ruoyi.ma.service.IMaMaterialProgressDetailsService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 材料进度详情Service业务层处理
 *
 * @author llb
 * @date 2024-11-01
 */
@RequiredArgsConstructor
@Service
public class MaMaterialProgressDetailsServiceImpl implements IMaMaterialProgressDetailsService {

    private final MaMaterialProgressDetailsMapper baseMapper;
    private final IMaMaterialSupplierService iMaMaterialSupplierService;
    private final MaMaterialSupplierMapper supplierMapper;
    private final IMaMaterialManifestService iMaMaterialManifestService;
    private final MaMaterialManifestMapper materialManifestMapper;

    /**
     * 查询材料进度详情
     */
    @Override
    public MaMaterialProgressDetailsVo queryById(Long progressDetailsId){
        return baseMapper.selectVoById(progressDetailsId);
    }

    /**
     * 查询材料进度详情列表
     */
    @Override
    public TableDataInfo<MaMaterialProgressDetailsVo> queryPageList(MaMaterialProgressDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaMaterialProgressDetails> lqw = buildQueryWrapper(bo);
        Page<MaMaterialProgressDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询材料进度详情列表
     */
    @Override
    public List<MaMaterialProgressDetailsVo> queryList(MaMaterialProgressDetailsBo bo) {
        LambdaQueryWrapper<MaMaterialProgressDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaMaterialProgressDetails> buildQueryWrapper(MaMaterialProgressDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaMaterialProgressDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanDetailsId() != null, MaMaterialProgressDetails::getPlanDetailsId, bo.getPlanDetailsId());
        lqw.eq(bo.getBdId() != null, MaMaterialProgressDetails::getBdId, bo.getBdId());
        lqw.eq(bo.getGysId() != null, MaMaterialProgressDetails::getGysId, bo.getGysId());
        lqw.eq(bo.getType() != null, MaMaterialProgressDetails::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getManifestCode()), MaMaterialProgressDetails::getManifestCode, bo.getManifestCode());
        lqw.like(StringUtils.isNotBlank(bo.getManifestName()), MaMaterialProgressDetails::getManifestName, bo.getManifestName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), MaMaterialProgressDetails::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), MaMaterialProgressDetails::getSpecification, bo.getSpecification());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), MaMaterialProgressDetails::getUnit, bo.getUnit());
        lqw.eq(bo.getMonthPlanPurchaseNumber() != null, MaMaterialProgressDetails::getMonthPlanPurchaseNumber, bo.getMonthPlanPurchaseNumber());
        lqw.eq(bo.getOnceSupplyNumber() != null, MaMaterialProgressDetails::getOnceSupplyNumber, bo.getOnceSupplyNumber());
        lqw.eq(bo.getMonthTotalSupplyNumber() != null, MaMaterialProgressDetails::getMonthTotalSupplyNumber, bo.getMonthTotalSupplyNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getAcceptanceCode()), MaMaterialProgressDetails::getAcceptanceCode, bo.getAcceptanceCode());
        lqw.eq(bo.getSupplyDate() != null, MaMaterialProgressDetails::getSupplyDate, bo.getSupplyDate());
        return lqw;
    }

    /**
     * 新增材料进度详情
     */
    @Override
    public Boolean insertByBo(MaMaterialProgressDetailsBo bo) {
        MaMaterialProgressDetails add = BeanUtil.toBean(bo, MaMaterialProgressDetails.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProgressDetailsId(add.getProgressDetailsId());
        }
        return flag;
    }

    /**
     * 修改材料进度详情
     */
    @Override
    public Boolean updateByBo(MaMaterialProgressDetailsBo bo) {
        MaMaterialProgressDetails update = BeanUtil.toBean(bo, MaMaterialProgressDetails.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaMaterialProgressDetails entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除材料进度详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取详情列表不分页
     *
     * @param bo
     * @return
     */
    @Override
    public List<MaMaterialProgressDetailsVo> allList(MaMaterialProgressDetailsBo bo) throws ParseException {
        LambdaQueryWrapper<MaMaterialProgressDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGysId() != null, MaMaterialProgressDetails::getGysId, bo.getGysId());
        if (StringUtils.isNotBlank(bo.getSupplyDate())) {
            // 供货日期处理
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date supplyDate = sdf.parse(bo.getSupplyDate());
            lqw.like(bo.getSupplyDate() != null, MaMaterialProgressDetails::getSupplyDate, supplyDate);
        }
        lqw.orderByDesc(MaMaterialProgressDetails::getSupplyDate);
        // 获取该站点供应商所有的材料清单, 供应商id和供货日期
        MaMaterialManifestBo manifestBo = new MaMaterialManifestBo();
        manifestBo.setGysId(bo.getGysId());
        R<List<MaMaterialManifestVo>> manifestListR = iMaMaterialManifestService.allList(manifestBo);
        List<MaMaterialManifestVo> manifestList = manifestListR.getData();
        List<MaMaterialProgressDetailsVo> data = new ArrayList<>();
        manifestList.forEach(item -> {
            MaMaterialProgressDetailsVo entity = BeanUtil.copyProperties(item, MaMaterialProgressDetailsVo.class);
//            entity.setManifestId(item.getManifestId());
            data.add(entity);
        });
        // @TODO 根据供应商id和供货日期查询材料计划详情表, 相同清单设置签收详情的供货数量
        LambdaQueryWrapper<MaMaterialPlanDetails> planLqw = new LambdaQueryWrapper<>();
        return data;
    }

    @Override
    public boolean updateDetails(MaMaterialProgressDataDetailsReturnVo bo) {
        // @TODO 更新
        MaMaterialProgressVo infoData = bo.getInfoData(); // 主表
        List<MaMaterialProgressDetailsVo> detailList = bo.getDetailList(); // 子表
        // 保存主表
        // 保存子表
        return true;
    }

    // 获取材料签收对应的计划详情，计算数量
    public void handlPlanNum() {

    }
}
